package com.tbsoft.ehr.action.resume;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.tbsoft.ehr.action.BaseAction;
import com.tbsoft.ehr.model.ProjectExperience;
import com.tbsoft.ehr.model.Resume;
import com.tbsoft.ehr.service.resume.ResumeService;

/**
 * @desc 简历管理Action
 * @author vastdream
 * 
 */
public class ResumeAction extends BaseAction {
	private Log log = LogFactory.getLog(getClass());
	private Resume resume;
	private List<ProjectExperience> projectExperienceList = new ArrayList<ProjectExperience>();

	public List<ProjectExperience> getProjectExperienceList() {
		return projectExperienceList;
	}

	public void setProjectExperienceList(
			List<ProjectExperience> projectExperienceList) {
		this.projectExperienceList = projectExperienceList;
	}

	private List<Resume> listResume = new ArrayList<Resume>();

	public List<Resume> getListResume() {
		return listResume;
	}

	public Resume getResume() {
		return resume;
	}

	public void setResume(Resume resume) {
		this.resume = resume;
	}

	private static final long serialVersionUID = 1L;
	private ResumeService resumeService;

	public String save() {
		try {
			resume.setProjectExperienceList(projectExperienceList);
			if ("".equals(resume.getId())) {
				resume.setId(null);
				resume.setUserId(readUserId());
				resumeService.saveResume(resume, getAttachFileList());
			}else{
				resumeService.savrOrupdateResume(resume, getAttachFileList());
			}
		} catch (Exception e) {
			log.error("save Exception", e);
			return ERROR;
		}
		setCallbackType("closeCurrent");
		setRel("resume_browse");
		return SUCCESS;
	}

	public String list() {
		int pageNum = this.getPageNum() > 0 ? this.getPageNum() - 1 : 0;
		int startIndex = pageNum * getNumPerPage();
		listResume.clear();
		try {
			List<Resume> tmpList = resumeService.getResumeList(resume,
					startIndex, getNumPerPage());
			int count = resumeService.getResumeCount(resume);
			setTotalCount(count);
			setPageNumShown((getNumPerPage() + count) / getNumPerPage());
			if (tmpList != null) {
				listResume.addAll(tmpList);
			}
		} catch (Exception e) {
			log.error("list Exception", e);
		}
		return "list";
	}

	/**
	 * @desc 打开编辑页面
	 * @return
	 */
	public String edit() {
		try {
			if (resume != null && resume.getId() != null
					&& !resume.getId().isEmpty()) {
				resume = resumeService.getResumeById(resume.getId());
				projectExperienceList = resume.getProjectExperienceList();
			}
		} catch (Exception e) {
			log.error("edit exception", e);
		}
		return "add";
	}

	public String delete() {
		try {
			if (resume != null && resume.getId() != null
					&& !resume.getId().isEmpty()) {
				resumeService.deleteResume(resume);
			}
		} catch (Exception e) {
			log.error("delete exception", e);
		}
		setRel("resume_browse");
		return SUCCESS;
	}

	public String view() {
		try {
			if (resume != null && resume.getId() != null
					&& !resume.getId().isEmpty()) {
				resume = resumeService.getResumeById(resume.getId());
				// 显示的时候对换行符进行处理
				resume.setOccupationalCharacteristics(resume
						.getOccupationalCharacteristics().replaceAll("\n",
								"<br/>"));
				resume.setSelfEvaluation(resume.getSelfEvaluation().replaceAll(
						"\n", "<br/>"));
				resume.setTechnicalExpertise(resume.getTechnicalExpertise()
						.replaceAll("\n", "<br/>"));
				projectExperienceList = resume.getProjectExperienceList();
				for (ProjectExperience pe : projectExperienceList) {
					pe
							.setJobDuties(pe.getJobDuties().replaceAll("\n",
									"<br/>"));
					pe.setMainTechnical(pe.getMainTechnical().replaceAll("\n",
							"<br/>"));
					pe.setProjectSummary(pe.getProjectSummary().replaceAll(
							"\n", "<br/>"));
				}
			}
		} catch (Exception e) {
			log.error("edit exception", e);
		}
		return "view";
	}

	public void setResumeService(ResumeService resumeService) {

		this.resumeService = resumeService;
	}

	public String addInterviewResult() {
		try {
			if (resume != null && resume.getId() != null
					&& !resume.getId().isEmpty()) {
				resume = resumeService.getResumeById(resume.getId());
			}
		} catch (Exception e) {
			log.error("addInterviewResult exception", e);
		}
		return "addInterviewResult";
	}

	public String saveInterviewResult(){
		try {
			resumeService.saveInterviewResult(resume);
		} catch (Exception e) {
			log.error("save Exception", e);
			return ERROR;
		}
		setCallbackType("closeCurrent");
		setRel("resume_browse");
		return SUCCESS;
	}
}
